@extends('common.base-master')

@section('html-head')
    <link rel="stylesheet" type="text/css" href="{{url('temple/css/index.css')}}">
    <script type="text/javascript" src="{{url('temple/js/jquery-1.7.2.js')}}"></script>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
@endsection

@section('html-body')
    <div>
        <input type="text" id="order_num" />
        <button id="btn">支付</button>
    </div>
    <script>
        $(function () {
            $('#btn').click(function () {
                var orderNum = $('#order_num').val();
                $.ajax({
                    type:"post",
                    url:'{{route('Api.Payment.payByWechatJsApi')}}',
                    dataType:"json",
                    data:{
                        order_num:orderNum
                    },
                    success:function(obj){
                        if(obj.level == "success"){
                            WXPayment(obj.data);
                        }else {
                            alert('fail');
                        }
                    },
                    error:function(obj){
                        console.log(obj);
                    }
                });
            });

            function WXPayment(configJSON) {
                if (typeof WeixinJSBridge === 'undefined') {
                   alert('fail');
                    return false;
                }
                WeixinJSBridge.invoke('getBrandWCPayRequest', configJSON, function (res) {
                    switch (res.err_msg) {
                        case 'get_brand_wcpay_request:cancel':
                            alert('用户取消支付！');
                            break;
                        case 'get_brand_wcpay_request:fail':
                            alert('支付失败！（' + res.err_desc + '）');
                            break;
                        case 'get_brand_wcpay_request:ok':
                            alert('支付成功');
                            break;
                        default:
                            alert(JSON.stringify(res));
                            break;
                    }
                });
            }
        })
    </script>
@endsection